<template>
	<view>
		<view class="search-box">
			<my-search @click="gotosearch"></my-search>
		</view>
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,i) in swiperList" :key="item.goods_id">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<img :src="item.image_src" alt="">
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 导航 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
				<img class='nav-img' :src="item.image_src" alt="">
			</view>
		</view>
		<!-- 楼层列表 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item,i) in floorList" :key="i">
				<!-- 楼层title -->
				<img class='floor-title' :src="item.floor_title.image_src" alt="">
				<!-- 楼层图片 -->
				<view class="floor-img-box">
					<!-- 左边的大图 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<img :src="item.product_list[0].image_src"
							:style="{width:item.product_list[0].image_width + 'rpx'}" mode='widthFix' alt="">
					</navigator>
					<!-- 右侧的小图 -->
					<view class="right-img-box">
						<navigator class="rigth-img-item" v-for="(item2,i2) in item.product_list.slice(1)" :key="i2"
							:url="item2.url">
							<img :src="item2.image_src" :style="{width:item2.image_width + 'rpx'}" mode='widthFix'
								alt="">
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	// 导入自己封装的 mixin 模块
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		// 将 badgeMix 混入到当前的页面中进行使用
		mixins: [badgeMix],
		data() {
			return {
				swiperList: [],
				navList: [],
				floorList: [],
			};
		},
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},

		methods: {
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/home/swiperdata')
				console.log(res)
				// 请求失败
				// if (res.meta.status !== 200) {
				// 	return uni.showToast({
				// 		title: '数据请求失败！',
				// 		duration: 1500,
				// 		icon: 'none'
				// 	})
				// }
				if (res.meta.status !== 200) return uni.$showMsg()
				this.swiperList = res.message
			},
			async getNavList() {
				const {
					data: res
				} = await uni.$http.get('/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.navList = res.message
			},
			navClickHandler(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()
				// 数据处理
				res.message.forEach(floor => {
					floor.product_list.forEach(pro => {
						let questy = pro.navigator_url.split('?')[1]
						pro.url = '/subpkg/goods_list/goods_list?' + questy
					})
				})
				this.floorList = res.message
			},
			gotosearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		// 设置定位效果的吸顶
		position: sticky;
		top: 0;
		z-index: 9;
	}

	swiper {
		height: 330rpx;

		.swiper-item,
		img {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-evenly;
		margin: 32rpx 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-list {
		padding-left: 8px;

		.floor-title {
			height: 60rpx;
			width: 100%;
		}

		.floor-img-box {
			display: flex;
		}

		.right-img-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-evenly;
		}
	}
</style>
